package com.tianlu.store.common;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;

@ApiModel(
        value = "返回结果封装",
        description = "response"
)
@JsonInclude
public class ResponseResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty("返回内容")
    private T data;
    @ApiModelProperty("状态码")
    private Integer code;
    @ApiModelProperty("返回错误信息")
    private String msg;
    @ApiModelProperty("返回用户信息")
    private String message;

    public static ResponseResult success() {
        return new ResponseResult((Object)null, ResultCode.CODE_200.code, ResultCode.CODE_200.msg, ResultCode.CODE_200.msg);
    }

    public static <T> ResponseResult<T> success(T o) {
        return new ResponseResult(o, ResultCode.CODE_200.code, ResultCode.CODE_200.msg, ResultCode.CODE_200.msg);
    }

    public static ResponseResult fail(Integer code) {
        return new ResponseResult((Object)null, code, "请求失败", "请求失败");
    }

    public static ResponseResult fail(Integer code, String message) {
        return new ResponseResult((Object)null, code, message, message);
    }

    public static ResponseResult fail(Integer code, String errorMessage, String userMessage) {
        return new ResponseResult((Object)null, code, errorMessage, userMessage);
    }

    public static ResponseResult fail(ResultCode resultCode) {
        return new ResponseResult((Object)null, resultCode.code, resultCode.msg, resultCode.msg);
    }

    public static ResponseResult fail(ResultCode resultCode, String appendMessage) {
        return new ResponseResult((Object)null, resultCode.code, resultCode.msg + "，" + appendMessage, resultCode.msg + "，" + appendMessage);
    }

    public static ResponseResult fail(ResultCode resultCode, String errorMessage, String userMessage) {
        return new ResponseResult((Object)null, resultCode.code, StrUtil.isEmpty(errorMessage) ? resultCode.msg : errorMessage, StrUtil.isEmpty(userMessage) ? resultCode.msg : userMessage);
    }

    public boolean check() {
        return !ResultCode.CODE_200.code.equals(this.getCode());
    }

    public boolean checkAll() {
        return !ResultCode.CODE_200.code.equals(this.getCode()) || this.getData() == null;
    }

    public static <T> ResponseResult<T> parse(String jsonStr, Class<T> o) {
        ResponseResult responseResult = new ResponseResult();
        JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
        responseResult.setCode(jsonObject.getInt("code"));
        responseResult.setMessage(jsonObject.getStr("message"));
        responseResult.setMsg(jsonObject.getStr("msg"));
        responseResult.setData(jsonObject.get("data", o));
        return responseResult;
    }

    public static ResponseResult getFallbackResponse(String service) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(ResultCode.CODE_500.code);
        responseResult.setMessage("无法连接" + service + "服务，该服务可能出现异常或已断开连接");
        return responseResult;
    }

    public static ResponseResult getFallbackResponse(String service, String message) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(ResultCode.CODE_500.code);
        responseResult.setMessage(service + "服务异常，异常消息：" + (StrUtil.isEmpty(message) ? "" : message));
        return responseResult;
    }

    public T getData() {
        return this.data;
    }

    public Integer getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public String getMessage() {
        return this.message;
    }

    public void setData(final T data) {
        this.data = data;
    }

    public void setCode(final Integer code) {
        this.code = code;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    public void setMessage(final String message) {
        this.message = message;
    }

    public ResponseResult() {
    }

    public ResponseResult(final T data, final Integer code, final String msg, final String message) {
        this.data = data;
        this.code = code;
        this.msg = msg;
        this.message = message;
    }
}
